package com.springdoor.web.controller.goods;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.springdoor.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springdoor.common.annotation.Log;
import com.springdoor.common.core.controller.BaseController;
import com.springdoor.common.core.domain.AjaxResult;
import com.springdoor.common.enums.BusinessType;
import com.springdoor.goods.domain.SprdGoodsMain;
import com.springdoor.goods.service.ISprdGoodsMainService;
import com.springdoor.common.utils.poi.ExcelUtil;
import com.springdoor.common.core.page.TableDataInfo;

/**
 * 商品主体信息Controller
 *
 * @author llshs
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/goods/main")
public class SprdGoodsMainController extends BaseController
{
    @Autowired
    private ISprdGoodsMainService sprdGoodsMainService;

    /**
     * 查询商品主体信息列表
     */
    @PreAuthorize("@ss.hasPermi('goods:main:list')")
    @GetMapping("/list")
    public TableDataInfo list(SprdGoodsMain sprdGoodsMain)
    {
        startPage();
        List<SprdGoodsMain> list = sprdGoodsMainService.selectSprdGoodsMainList(sprdGoodsMain);
        return getDataTable(list);
    }

    /**
     * 导出商品主体信息列表
     */
    @PreAuthorize("@ss.hasPermi('goods:main:export')")
    @Log(title = "商品主体信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SprdGoodsMain sprdGoodsMain)
    {
        List<SprdGoodsMain> list = sprdGoodsMainService.selectSprdGoodsMainList(sprdGoodsMain);
        ExcelUtil<SprdGoodsMain> util = new ExcelUtil<SprdGoodsMain>(SprdGoodsMain.class);
        util.exportExcel(response, list, "商品主体信息数据");
    }

    /**
     * 获取商品主体信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:main:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return success(sprdGoodsMainService.selectSprdGoodsMainByGoodsId(goodsId));
    }

    /**
     * 获取商品主体信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:main:query')")
    @GetMapping(value = "/findMain/{gNumber}")
    public AjaxResult getInfoByGNumber(@PathVariable("gNumber") String gNumber)
    {
        return success(sprdGoodsMainService.selectSprdGoodsMainByGNumber(gNumber));
    }

    /**
     * 新增商品主体信息
     */
    @PreAuthorize("@ss.hasPermi('goods:main:add')")
    @Log(title = "商品主体信息->新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SprdGoodsMain sprdGoodsMain)
    {
        sprdGoodsMain.setCreateBy(getUsername());
        sprdGoodsMain.setUpdateBy(getUsername());
        if (sprdGoodsMainService.insertSprdGoodsMain(sprdGoodsMain) >= 1) {
            Object gNumber = sprdGoodsMain.getgNumber();
            return success(gNumber);
        }
        return toAjax(0);
    }

    /**
     * 修改商品主体信息
     */
    @PreAuthorize("@ss.hasPermi('goods:main:edit')")
    @Log(title = "商品主体信息->修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SprdGoodsMain sprdGoodsMain)
    {
        sprdGoodsMain.setUpdateBy(getUsername());
        return toAjax(sprdGoodsMainService.updateSprdGoodsMain(sprdGoodsMain));
    }


    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('goods:main:remove')")
    @Log(title = "商品主体信息->删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gNumber}")
    public AjaxResult deleteGoodsByGnumber(@PathVariable("gNumber")String gNumber) {
        return toAjax(sprdGoodsMainService.deleteGoodsByGnumber(gNumber));
    }

    @PreAuthorize("@ss.hasPermi('goods:main:edit')")
    @GetMapping("/findMaxOrderNum/{assNumber}")
    public AjaxResult findMaxOrderNum(@PathVariable String assNumber) {
        return success(sprdGoodsMainService.findMaxOrderNum(assNumber));
    }


    @PreAuthorize("@ss.hasPermi('goods:main:query')")
    @GetMapping("/getAssortInfo/{gNumber}")
    public AjaxResult getAssortInfo(@PathVariable String gNumber) {
        return success(sprdGoodsMainService.getAssortInfo(gNumber));
    }

    @PreAuthorize("@ss.hasPermi('goods:shop:list')")
    @GetMapping("/listNotinShop")
    public TableDataInfo selectGoodsMainListNotinShop(SprdGoodsMain sprdGoodsMain) {
        startPage();
        List<SprdGoodsMain> list = sprdGoodsMainService.selectGoodsMainListNotinShop(sprdGoodsMain);
        return getDataTable(list);
    }

    /**
     * 查询商品主体信息列表
     */
    /*@PreAuthorize("@ss.hasPermi('goods:main:query')")
    @GetMapping("/optionSelect")
    public AjaxResult optionSelect()
    {
        SprdGoodsMain main = new SprdGoodsMain();
        main.setParentId(0l);
        main.setDelFlag("0");
        List<SprdGoodsMain> list = sprdGoodsMainService.selectSprdGoodsMainList(main);
        return success(list);
    }*/
}
